﻿using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Text.RegularExpressions;
using Model;

namespace FubiManger.UI
{
    partial class AddfreshfoodForm : Smobiler.Core.Controls.MobileForm
    {
        public AddfreshfoodForm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        List<ms_goods> gdlist = new List<ms_goods>();
        ms_goods gd = new ms_goods();
        /// <summary>
        /// 商户ID
        /// </summary>
        public string merchantid { get; set; }
        /// <summary>
        /// 0正常1测试
        /// </summary>
        public string sell = "0";
        private void panclose_Press(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtprice_TextChanged(object sender, EventArgs e)
        {

            string result = "";
            try
            {
                result = txtprice.Text;
                string pattern = @"[^0-9.]+|(?<=\.\d{2})\d+"; // 正则表达式模式
                string replacement = ""; // 替换为空字符串

                // 调用Regex.Replace函数并传递参数
                txtprice.Text = Regex.Replace(result, pattern, replacement);
            }
            catch (Exception)
            {

            }
        }

        private void txtprice_TouchLeave(object sender, EventArgs e)
        {
            
        }

        private void AddfreshfoodForm_Load(object sender, EventArgs e)
        {
            
            gdlist = goodsDal.Queryallbymid(merchantid);//查商户所有上架商品
            PopListGroup pg = new PopListGroup();
            for (int i = 0; i < gdlist.Count; i++)
            {
                PopListItem item = new PopListItem();
                item.Text = gdlist[i].goods_name;
                item.Value = gdlist[i].id;
                pg.Items.Add(item);
            }
            popgoods.Groups.Add(pg);
        }

        private void popgoods_Selected(object sender, EventArgs e)
        {
            btnname.Text = popgoods.Selection.Text;//获取商品名称
            gd = goodsDal.Querybyid(popgoods.Selection.Value);//通过商品ID查详情
            txtprice.Text = gd.price;
        }

        private void btnname_Press(object sender, EventArgs e)
        {
            popgoods.Show();
        }

        private void btnsave_Press(object sender, EventArgs e)
        {
            if (fresh_goodsDal.Addgood(gd.goods_name, gd.id, merchantid, txtprice.Text, gd.cover,sell) > 0)
            {
                this.Close();
            }
        }

        private void cbosell_CheckedChanged(object sender, EventArgs e)
        {
            if (cbosell.Checked == true)
                sell = "1";
            else
                sell = "0";
        }
    }
}